/**
 * Created by icyhy on 2017/5/23.
 */
"use strict";
var builder = require("../response/rest.result.builder.js");
var adModel = require("../models/advertisement");

module.exports = {

  create : function (req, res, next) {
    var adEnity = new adModel(req.body);
    console.log(adEnity);
    adModel.create(adEnity, function(err) {
      if (err) return next(err);

      return builder.buildSuccessResultInfo(res);
    });
  },
  getList:function(req, res, next){
    adModel.find().exec(function(err, docs) {
      if (err) return next(err);
      if (!docs) return next(new Error('doc not found'));

      return builder.buildSuccessResultInfoWithData(res,docs);
    });
  },
  /**
   * 获取广告
   */
  getById:function(req, res, next){
    var advId = req.params.id || '';
    adModel.findOne({
      _id: advId
    })
      .exec(function(err, ad) {
        if (err) return next(err);
        if (!ad) return next(new Error('not  found'));

        return builder.buildSuccessResultInfo(res, ad);
      });
  },
  /**
   * 删除广告
   */
  deleteById:function (req, res, next) {
    var advId = req.params.id || '';
    adModel.remove({ _id: advId }, function (err) {
      if (err) return next(err);
      return builder.buildSuccessResultInfo(res);
    });
  },
}
